package logic

import (
	"context"
	"errors"
	"strings"
	"videos/model/mysql"
	"videos/videorpc/internal/svc"
	"videos/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	if in.Content == "" {
		return nil, errors.New("请输入评论内容")
	}

	if strings.Contains(in.Content, "垃圾") || strings.Contains(in.Content, "骗人") {
		return nil, errors.New("含违规词汇,请修改")
	}

	comment := mysql.Comment{
		Content: in.Content,
		VideoId: int(in.VideoId),
		UserId:  int(in.UserId),
	}

	err := comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &videorpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
